import baseUrl from './baseUrlShop.js'
import utils from './utils.js'

module.exports = {
	request(url, data = {}, method = "POST", loading = true) {
		// let lastTime = 1671073308
		// let time = Math.floor(new Date().getTime() / 1000)
		// if (lastTime - time <= 0) {
		// 	uni.showToast({
		// 		title: "请求失败",
		// 		icon: "none"
		// 	})
		// 	return
		// }
		if (loading) {
			uni.showLoading({
				title: "加载中",
				mask: true,
			});
		}
		const token = uni.getStorageSync("token");
		// 门店登录后token
		const storeToken = uni.getStorageSync("store_token");
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data,
				method,
				header: {
					token: token || "",
					storeToken: storeToken || '',
					"Content-type": "application/x-www-form-urlencoded",
				},
				success: (resp) => {
					if (url == "/index/name_share" || url == "/index/team_details") {
						console.log("resp======", resp);
					}
					uni.hideLoading();
					if (resp.data.code !== 200) {
						utils.toast(resp.data.msg)
						reject(resp.data)
						return
					}
					resolve(resp.data);
				},
				fail: (error) => {
					uni.hideLoading();
					reject(error);
				},
				complete: () => {},
			});
		});
	},
};